package com.ldoublem.loadingviewlib;


import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;



public class LVCircularCD extends Component implements Component.EstimateSizeListener, Component.DrawTask {

    private Paint mPaint;

    private float mWidth = 0f;
    private float mPadding = 0f;

    AnimatorProperty mProgerssRotateAnim;
    RectFloat rectF = new RectFloat();
    RectFloat rectF2 = new RectFloat();
    public LVCircularCD(Context context) {
        this(context, null);
    }

    public LVCircularCD(Context context, AttrSet attrs) {
        this(context, attrs, 0);
    }

    public LVCircularCD(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }





    private void initPaint() {
        setEstimateSizeListener(this);
        addDrawTask(this);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE_STYLE);
        mPaint.setColor(Color.WHITE);
        mProgerssRotateAnim = this.createAnimatorProperty();
        mProgerssRotateAnim.rotate(360);
        mProgerssRotateAnim.setLoopedCount(-1);

    }

    public void setViewColor(int color)
    {
        mPaint.setColor(new Color(color));
        invalidate();
    }



    public void startAnim() {
        stopAnim();
        mProgerssRotateAnim.setDuration(1500);
        mProgerssRotateAnim.start();
    }

    public void startAnim(int time) {
        stopAnim();
        mProgerssRotateAnim.setDuration(time);
        mProgerssRotateAnim.start();
    }

    public void stopAnim() {
        mProgerssRotateAnim.cancel();
    }


    @Override
    public void onDraw(Component component, Canvas canvas) {
        canvas.save();
        mPaint.setStrokeWidth(2);
        canvas.drawCircle(mWidth / 2, mWidth / 2, mWidth / 2 - mPadding, mPaint);
        mPaint.setStrokeWidth(3);
        canvas.drawCircle(mWidth / 2, mWidth / 2, mPadding, mPaint);

        mPaint.setStrokeWidth(2);
        rectF = new RectFloat(mWidth / 2 - mWidth / 3, mWidth / 2 - mWidth / 3,
                mWidth / 2 + mWidth / 3, mWidth / 2 + mWidth / 3);
        canvas.drawArc(rectF, new Arc(0, 80
                , false), mPaint);

        canvas.drawArc(rectF, new Arc(180, 80
                , false), mPaint);


        rectF2 = new RectFloat(mWidth / 2 - mWidth / 4, mWidth / 2 - mWidth / 4,
                mWidth / 2 + mWidth / 4, mWidth / 2 + mWidth / 4);
        canvas.drawArc(rectF2, new Arc(0, 80
                , false), mPaint);
        canvas.drawArc(rectF2, new Arc(180, 80
                , false), mPaint);
        canvas.restore();
    }

    @Override
    public boolean onEstimateSize(int i, int i1) {
        if (EstimateSpec.getSize(i) > getHeight())
            mWidth = EstimateSpec.getSize(i1);
        else
            mWidth = EstimateSpec.getSize(i);
        mPadding = 5;
        return false;
    }
}
